<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/20
 * Time: 4:11
 */
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

class User extends ActiveRecord implements IdentityInterface{

    public $id;
    public $username;
    public $password;
    public $sex;
    public $birth;
    public $remark;
    public $image;
    public static function tableName()
    {
        return 'user';
    }

    public function rules()
    {
        return [[["username","password","sex","birth","image"],"required",],
            ['username', 'string', 'length' => [6, 20],'message'=>'用户名长度6-20个字符',],
            ['username', 'unique', 'message' => '用户名已存在.','on'=>'register'],
            ['password', 'string', 'length' => [6, 20]],
        ['image', 'file', 'extensions' => ['png', 'jpg', 'gif'],'message' => '请上传正确的文件格式',],
        ];

    }
    public function scenarios()
    {
        return [
            'login' => ['username', 'password'],
            'register' => ['username', 'password','sex','birth','image','remark'],
        ];
    }
    public static function findIdentity($id)
    {
        return static::findOne($id);
    }

    /**
     * 根据 token 查询身份。
     *
     * @param string $token 被查询的 token
     * @return IdentityInterface|null 通过 token 得到的身份对象
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {

    }

    /**
     * @return int|string 当前用户ID
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return string 当前用户的（cookie）认证密钥
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

    /**
     * @param string $authKey
     * @return boolean if auth key is valid for current user
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }




}